package org.contact.dao.impl;

import java.util.List;
import java.util.Map;

import org.contact.dao.EmpresaDAO;
import org.contact.model.dto.EmpresaDTO;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class EmpresaDAOImpl extends HibernateDaoSupport implements
		EmpresaDAO {
	
	public EmpresaDAOImpl(SessionFactory sessionFactory) {
		this.setSessionFactory(sessionFactory);
	}

	@Override
	public void delete(EmpresaDTO t) {
		// TODO Auto-generated method stub
		getHibernateTemplate().delete(t);
	}

	@Override
	public EmpresaDTO getById(EmpresaDTO t) {
		// TODO Auto-generated method stub
		return (EmpresaDTO)getHibernateTemplate().get(EmpresaDTO.class, t.getId_empresa());
	}

	@Override
	public Long insert(EmpresaDTO t) {
		// TODO Auto-generated method stub
		return (Long)getHibernateTemplate().save(t);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<EmpresaDTO> loadAll(Map<String, String> filter) {
		// TODO Auto-generated method stub
		List<EmpresaDTO> list = null;
		if(filter == null){
			list = getHibernateTemplate().loadAll(EmpresaDTO.class);
			return list;
		}else {
			String sql = "SELECT * FROM tb_empresa WHERE lower(" + filter.get("col") + ") ";
			sql = sql + "LIKE '%" + filter.get("val").toLowerCase() + "%';";
			System.out.println(sql);
			list = getSession().createSQLQuery(sql)
				.addEntity(EmpresaDTO.class)
				.list();
			return list;
		}
	}

	@Override
	public void update(EmpresaDTO t) {
		// TODO Auto-generated method stub
		getHibernateTemplate().update(t);
	}


}
